var app = getApp();
var linkUrl = "";
import drawQrcode from '../../utils/weapp.qrcode.esm.js'
Page({
  data:{
    showView:false,
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    },
    imageUrl:"",
  },

  onLoad() {
    var that = this;
    //获得用户信息
    wx.getUserInfo({
      success: function (res) {
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          [avatarUrl]: res.userInfo.avatarUrl,
          [nickName]: res.userInfo.nickName,
        });
      }
    });
  },
  onShareAppMessage: function () {
    return {
      title: '注册',
      path: '/pages/register/register?' + url1,
      success: function (res) {

      }
    }
  },
  //邀请好友
  show: function () {
    var that=this;

    this.setData({
      showView: (!this.data.showView)
    });
    /*wx.request({
      url: 'http://localhost/generateInvitationCode',
      type: "post",
      dataType: "json",
      success: function (data) {
        var invitationCode = data.data;
        linkUrl += invitationCode;
*/
        //生成二维码
        drawQrcode({
          width: 200,
          height: 200,
          canvasId: 'myCanvas',
          text: linkUrl,
          image: {
            imageResource: "../../images/bu.jpg",
            dx: 80,
            dy: 80,
            dWidth: 30,
            dHeight: 30
          }
        });

        //生成图片
        const ctx = wx.createCanvasContext('myCanvas');
        ctx.draw(true, setTimeout(function () {
          wx.canvasToTempFilePath({
            canvasId: 'myCanvas',
            fileType: 'jpg',
            success: function (res) {
              that.setData({
                imageUrl: res.tempFilePath
              });
            }, fail: function (res) {
              console.log(res);
            }
          })
        }, 1000));
      /*}
    });*/
    
  },
  save() {
    let that = this
    //若二维码未加载完毕，加个动画提高用户体验
    wx.showToast({
      icon: 'loading',
      title: '正在保存图片',
      duration: 1000
    })
    //判断用户是否授权"保存到相册"
    wx.getSetting({
      success(res) {
        //没有权限，发起授权
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {//用户允许授权，保存图片到相册
              that.savePhoto();
            },
            fail() {//用户点击拒绝授权，跳转到设置页，引导用户授权
              wx.openSetting({
                success() {
                  wx.authorize({
                    scope: 'scope.writePhotosAlbum',
                    success() {
                      that.savePhoto();
                    }
                  })
                }
              })
            }
          })
        } else {//用户已授权，保存到相册
          that.savePhoto()
        }
      }
    })
  },
  savePhoto() {
    let that = this;
    console.log("路径" + this.data.imageUrl);
        wx.saveImageToPhotosAlbum({
          filePath: this.data.imageUrl,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: "success",
              duration: 1000
            });
          }
        })
  },
  //复制链接
  copyText: function () {
    let that = this
    //获取剪切板内容
    wx.getClipboardData({
      success(res) {
        wx.setClipboardData({
          data: "链接",
          success(res) {
          }
        })
      }
    })
  },
  tiaoGoumai(){


  }
});